#from core import myapp
from bson.dbref import DBRef
from lib.mongodb import DBConn
from config import cfg
import time

#app = myapp


class TestData():
    
    def __init__(self):
        conn = DBConn()
        self.db = conn.get_database(cfg.database)

    def save(self, collection, _dict, additionals=None):
        '''Arguments:
            fields -- dict type, additional fields
        Return:
            The '_id' value of to_save or [None] if manipulate is False and to_save has no _id field.
        '''
        db = self.db
        model = db.find_one(collection, _dict)
        if model is None:
            if additionals is None: 
                return db.save(collection, _dict)
            else:
                _dict.update(additionals)
                return db.save(collection, _dict)
        else:
            return model['_id']
        
    def gen_datas(self):
        # The following are embedded datas, the embedded datas will be created when run tests
        # -------------------------------------
        # User
        # -------------------------------------
        user_dict = {'username':'embedded user', 
                       'password':'embedded user',
                       'email':'user@specxp.com',
                       'phone':'123-456-7890', 
                       'role': 1, #0 user, 1 admin
                       'title': 0 # 0 project manager
        }
        
        user_id = self.save('users', user_dict, {'created': time.time()})
        
        # -------------------------------------
        # Project
        # -------------------------------------
        project_dict = {'name': 'embedded specxp',
                        'description': 'This is a project',
                        'owner_id': user_id
                    }
        
        project_id = self.save('projects', project_dict, {'created': time.time()})
        
        # -------------------------------------
        # Sprint
        # -------------------------------------    
        sprint_dict = {'name':'embedded sprint-1', 
                       'description': 'This is specxp sprint-1',
                       'project_id':project_id}
        
        sprint_id = self.save('sprints', sprint_dict, {'created': time.time()})
        
        
        # -------------------------------------
        # Spec
        # -------------------------------------
        quick_spec_dict = {'name':'quick spec-1', 
               'description': 'This is not completed quick spec-1',
               'importance' :'0',
               'source':'0'}
        
        spec_dict = {'name':'embedded test spec-1', 
                       'description': 'This is specxp sprint-1 spec-1',
                       'importance' :'0',
                       'source':'0',
                       'sprint_id':sprint_id}
        
        spec_id = self.save('specs', quick_spec_dict, {'created': time.time(), 'checked_in': 0 })


        self.new_user = {'username':'test user', 
                       'password':'testuser',
                       'email':'testuser@specxp.com',
                       'phone':'123-456-7890', 
                       'role': 1, #0 user, 1 admin
                       'title': 0 # 0 project manager
                       }
        
        self.new_project = {'name': 'test project',
                        'description': 'This is a test project',
                        'owner_id':user_id}
        
        self.new_sprint = {'name':'test sprint', 
                       'description': 'This is a test sprint',
                       'project_id':project_id}
        
        
        self.new_spec = quick_spec_dict
        


test_data = TestData()
test_data.gen_datas()